package com.vf.admin.client.dao;

import com.vf.admin.common.db.BaseDao;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppAuth;
import com.vf.admin.miaodong.entity.AppAuthUser;
import com.vf.admin.utils.StringEx;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Repository
public class AppAuthUserDao extends BaseDao<AppAuthUser> {

    public List<AppAuthUser> page(Map<String, Object> paraMap, SplitPageBean splitPage) {
        ArrayList<Object> paramList = new ArrayList<>();
        StringBuilder sbStr = new StringBuilder("select * from app_auth_user where 1=1");
        if (StringEx.isNotNull(paraMap.get("userId"))) {
            sbStr.append(" and userId = ? ");
            paramList.add(paraMap.get("userId"));
        }
        if (StringEx.isNotNull(paraMap.get("authNameEn"))) {
            sbStr.append(" and authNameEn = ? ");
            paramList.add(paraMap.get("authNameEn"));
        }

        sbStr.append(" order by status asc ");
        String sql = sbStr.toString();
        return this.queryList(sql, paramList, AppAuthUser.class, splitPage);
    }

    public List<AppAuthUser> infoByUser(Integer userId) {
        ArrayList<Object> params = new ArrayList<>();

        StringBuilder sbStr = new StringBuilder("select * from app_auth_user where userId = ?");
        params.add(userId);

        return this.queryList(sbStr.toString(), params, AppAuthUser.class);
    }

    public AppAuthUser infoByUserAndName(Integer userId, String name) {
        ArrayList<Object> params = new ArrayList<>();

        StringBuilder sbStr = new StringBuilder("select u.* from app_auth_user u " +
                " left join app_user a " +
                " on u.userId = a.id " +
                " where (u.userId = ? or a.tengxuncode= ?) and u.authNameEn = ?");
        params.add(userId);
        params.add(userId);
        params.add(name);

        return this.queryObj(sbStr.toString(), params, AppAuthUser.class);
    }

    public AppAuthUser infoSuccessByUserAndName(Integer userId, String name) {
        ArrayList<Object> params = new ArrayList<>();

        StringBuilder sbStr = new StringBuilder("select u.* from app_auth_user u " +
                " left join app_user a " +
                " on u.userId=a.id " +
                " where (u.userId = ? or a.tengxuncode=?) and u.authNameEn = ? and u.status=2");
        params.add(userId);
        params.add(userId);
        params.add(name);

        return this.queryObj(sbStr.toString(), params, AppAuthUser.class);
    }

    public AppAuthUser infoByUserAndNameByUserIdOrTXCode(Integer userId, String name) {
        ArrayList<Object> params = new ArrayList<>();

        StringBuilder sbStr = new StringBuilder("select u.* from app_auth_user u " +
                " left join app_user a " +
                " on u.userId=a.id " +
                " where (u.userId = ? or a.tengxuncode=?) and u.authNameEn = ?");
        params.add(userId);
        params.add(userId);
        params.add(name);

        return this.queryObj(sbStr.toString(), params, AppAuthUser.class);
    }

    public void successByUserAndName(Integer userId, String name) {
        ArrayList<Object> params = new ArrayList<>();
        StringBuilder sbStr = new StringBuilder("select * from app_auth_user where userId = ? and authNameEn = ?");
        params.add(userId);
        params.add(name);

        AppAuthUser appAuthUser = this.queryObj(sbStr.toString(), params, AppAuthUser.class);
        appAuthUser.setStatus(2);

        this.update(appAuthUser);
    }

    public void rejectByUserAndName(Integer userId, String name) {
        ArrayList<Object> params = new ArrayList<>();
        StringBuilder sbStr = new StringBuilder("select * from app_auth_user where userId = ? and authNameEn = ?");
        params.add(userId);
        params.add(name);

        AppAuthUser appAuthUser = this.queryObj(sbStr.toString(), params, AppAuthUser.class);
        appAuthUser.setStatus(3);

        this.update(appAuthUser);
    }

    public void delByUserId(Integer userId) {
        String sql = "delete from app_auth_user where userId=" + userId;
        this.del(sql, new ArrayList<>());
    }
}
